var pageParamStr = $.url().param('pageParam');
var pageParam = JSON.parse(pageParamStr);

var gl_vue_vm_list_inventory_html = $('#vue_vm_list_inventory').html();

$(function() {
	$('#vue_vm_list_inventory div').hide();
    if (pageParam.type == 'month') {
        elderPayRecord_findMonthInventory(pageParam.beHospitalId,pageParam.yearMonth);
    } else if (pageParam.type == 'day') {
        elderPayRecord_findDayInventory(pageParam.beHospitalId,pageParam.yearMonthDay);
    } else if(pageParam.type == 'timeInterval'){
        elderPayRecord_findCostInventoryBybeHospitalId(pageParam.beHospitalId,pageParam.startTime,pageParam.endTime);
	} else if(pageParam.type == 'buda'){
        elderPayRecord_findByBeHospitalId(pageParam.beHospitalId);
    }
});

var elderPayRecord = {
	elderPayRecordId: '',
	costDesc: '',
	elderId: '',
	elderName: '',
	elderCardId: '',
	payMoney: '',
	payDate: '',
	createTime: '',
	updateTime: '',
	remark: '',
	payStatus: '',
	payWay: '',
	relationId: '',
	costType: '',
	subjectRoomId: '',
	payType: '',
	createId: getLoginedUser()? getLoginedUser().staffId: '',
	updateId: getLoginedUser()? getLoginedUser().staffId: '',
	subjectRoomId: getLoginedUser()? getLoginedUser().subjectRoomId: '',
	subjectRoomName: getLoginedUser()? getLoginedUser().subjectRoomName: ''
};

var view = {
	data: elderPayRecord
};

if($('#vue_vm').length > 0) {
	var vue_vm = new Vue({
		el: '#vue_vm',
		data: view
	});
}

function elderPayRecord_findByBeHospitalId(beHospitalId) {
    if ($('#vue_vm_list_inventory div').length == 0) {
        return;
    }
    $('#vue_vm_list_inventory div').hide();

    find_basic_by_json(path + '/elderPayRecord/findByBeHospitalId',
        {beHospitalId:beHospitalId}, function (response) {
            setData(response);
        });
}

function elderPayRecord_findCostInventoryBybeHospitalId(beHospitalId,startTime,endTime) {
    if ($('#vue_vm_list_inventory div').length == 0) {
        return;
    }
    $('#vue_vm_list_inventory div').hide();

    find_basic_by_json(path + '/elderPayRecord/findCostInventoryBybeHospitalId',
        {beHospitalId:beHospitalId, dateSince:startTime, dateEnd:endTime}, function (response) {
        setData(response);
    });
}

function elderPayRecord_findMonthInventory(beHospitalId,yearMonth) {
    if ($('#vue_vm_list_inventory div').length == 0) {
        return;
    }
    $('#vue_vm_list_inventory div').hide();

    find_basic_by_json(path + '/elderPayRecord/findMonthInventory', {
        month: yearMonth,
        beHospitalId: beHospitalId
    }, function (response) {
        setData(response);
    });
}

function elderPayRecord_findDayInventory(beHospitalId,yearMonthDay) {
    if ($('#vue_vm_list_inventory div').length == 0) {
        return;
    }
    $('#vue_vm_list_inventory div').hide();

    find_basic_by_json(path + '/elderPayRecord/findDayInventory', {
        day: yearMonthDay,
        beHospitalId: beHospitalId
    }, function (response) {
        setData(response);
    });
}

	function setData(response){
        $('#vue_vm_list_inventory').html(gl_vue_vm_list_inventory_html);

        new Vue({
            el: '#vue_vm_list_inventory',
            data: {
                todos: response.data
            },
            methods: {
                getPayRecordSummAndGroup:function(){
                    if(this.todos){
                        var  elderPayRecordForms = this.todos[0].elderPayRecordForms;
                        var summAndGroupArr = [];
                        for(var i=0;i<elderPayRecordForms.length;i++){
                            if(elderPayRecordForms[i].payWay==3){
                                continue;
                            }
                            var arrItem = {
                                costDesc:elderPayRecordForms[i].payWay==3?elderPayRecordForms[i].costDesc+"(VIP)":elderPayRecordForms[i].costDesc,
                                payMoney:elderPayRecordForms[i].payMoney/(elderPayRecordForms[i].quantity?elderPayRecordForms[i].quantity:1),
                                count:elderPayRecordForms[i].quantity?elderPayRecordForms[i].quantity:1
                            }
                            var isHas = false;
                            for(var j=0;j<summAndGroupArr.length;j++){
                                if(summAndGroupArr[j].costDesc==arrItem.costDesc&&summAndGroupArr[j].payMoney==arrItem.payMoney){
                                    summAndGroupArr[j].count = summAndGroupArr[j].count?summAndGroupArr[j].count+(elderPayRecordForms[i].quantity?elderPayRecordForms[i].quantity:1):1;
                                    isHas = true;
                                    break;
                                }
                            }
                            if(!isHas){
                                summAndGroupArr.push(arrItem);
                            }
                        }
                        return summAndGroupArr;
                    }
                },
                getTitle:function() {
                    if (pageParam.type == 'month') {
                        return '月份：'+pageParam.yearMonth;
                    } else if (pageParam.type == 'day') {
                        return '日期：'+pageParam.yearMonthDay;
                    }else if(pageParam.type == 'timeInterval'){
                        return formatDate(pageParam.startTime,'yyyy-MM-dd')+"至"+formatDate(pageParam.endTime,'yyyy-MM-dd');
					}else if(pageParam.type == 'buda'){
                        return formatDate(pageParam.startTime,'yyyy-MM-dd')+"至"+formatDate(pageParam.endTime,'yyyy-MM-dd');
                    }
                    return '未知日期';
                },
                openTotalPage:function() {
                    openTotalPageByTimeInterval(elderBeHospital.beHospitalId, startTime, endTime);
                },
                openPrintPage:function(paperType) {
                    openPrintPageByMonth(elderBeHospital.beHospitalId, yearMonth, paperType);
                },
                getNowDate: function() {
                    return getNowFormatDate().substr(0,10);
                },
                getElderPayRecordCount:function(forms) {
                    return forms.length;
                },
                getElderPayRecordVIPTotalMoney(forms){
                    var totalMoney = 0;
                    
                    if(forms) {
                        for(var i=0; i<forms.length; i++) {
                            if(forms[i].payWay==3){//如果是vip卡扣费
                                totalMoney += forms[i].payMoney;
                            }
                        }
                    }
                    return totalMoney;
                },
                getElderPayRecordTotalMoney:function(forms) {
                    var totalMoney = 0;

                    if(forms) {
                        for(var i=0; i<forms.length; i++) {
                            if(forms[i].payWay==3){//如果是vip卡扣费
                                continue;
                            }
                            totalMoney += forms[i].payMoney;
                        }
                    }
                    return totalMoney;
                },
                getElderArrearageTotalMoney:function(forms) {
                    var totalMoney = 0;

                    if(forms) {
                        for(var i=0; i<forms.length; i++) {
                            totalMoney += forms[i].arrearageMoney;
                        }
                    }

                    return totalMoney;
                }
            }
        });

        $('#vue_vm_list_inventory div').show();
	}
